package com.hongxun.budget.ui.activity.sg;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.XUtils;
import com.hongxun.greenentity.sg.CScheduleReportEntity;
import com.hongxun.interfaces.ClickListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 项目施工进度详情
 */
public class ScheduleReportListActivity extends BaseListActivity {
    @BindView(R.id.center_title)
    TextView tvTitle;
    @BindView(R.id.tv4_add)
    TextView tv4Add;
    @BindView(R.id.tv1)
    TextView tv1;

    private SuperAdapter superAdapter;
    private List<CScheduleReportEntity> datas = new ArrayList<>();

    @Override
    public int getMyListLayoutId() {
        return R.layout.sg_activity_schedule_report_list;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        tvTitle.setText(String.valueOf(SPUtils.get(context, SgStrKey.namexm, "进度详情")));
        String s1 = String.valueOf(SPUtils.get(context, SgStrKey.namejd, ""));
        String s2 = String.valueOf(SPUtils.get(context, SgStrKey.dpgqtime, ""));
        tv1.setText(s1 + "，倒排工期：" + s2);

        tv4Add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtils.put(context, SgStrKey.uuidjdjl, "");
                startActivity(new Intent(activity, ScheduleReportAddActivity.class)
                        .putExtra("isNewAdd", true));
            }
        });
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CScheduleReportEntity>(R.layout.sg_content_cs_pro_detail_adapter) {
            @Override
            public void bindView(ViewHolder viewHolder, CScheduleReportEntity entity, int i) {
                try {
                    ImageView imageView = viewHolder.getView(R.id.iv1);
                    Object ssUrl = SgDBUtils.getSsUrl(entity.getGuid());
                    XUtils.loadImg(context, ssUrl, imageView);
                    ((TextView) viewHolder.getView(R.id.tv1)).setText(entity.getSRTheme());
                    ((TextView) viewHolder.getView(R.id.tv2)).setText(entity.getSRUsername());
                    Date updatetime = entity.getSRUpdatetime();
                    if (updatetime != null && updatetime.getTime() != 0) {
                        ((TextView) viewHolder.getView(R.id.tv4)).setText(DateUtils.getDateToString(updatetime));
                    }
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setData(datas);
        superAdapter.setEmptyDataView(R.layout.view_empty);
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CScheduleReportEntity>() {
            @Override
            public void onClick(int i, CScheduleReportEntity o) {
                SPUtils.put(context, SgStrKey.uuidjdjl, o.getGuid());
                SPUtils.put(context, SgStrKey.namejd, o.getSName());
                startActivity(new Intent(activity, ScheduleReportAddActivity.class)
                        .putExtra("isNewAdd", false));
            }
        });
        superAdapter.setOnItemLongClickListener(new SuperAdapter.OnItemLongClickListener<CScheduleReportEntity>() {
            @Override
            public void onLongClick(int i, CScheduleReportEntity o) {
                longClickDel(o);
            }
        });
        return superAdapter;
    }

    //删除项目
    private void longClickDel(final CScheduleReportEntity o) {
        new NoticeDialog()
                .setTitle("删除")
                .setMsg("是否删除该项记录：" + o.getSRTheme())
                .setCancelButton("取消", null)
                .setConfirmButton("删除", new ClickListener() {
                    @Override
                    public void click() {
                        datas.remove(o);
                        superAdapter.notifyDataSetChanged();
                        SgDBUtils.delCScheduleReportEntity(o.getGuid());
                    }
                }).show(getFragmentManager(), "");
    }

    @Override
    public void onRefresh() {
        Observable.just(SgStrKey.uuidjd).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String value) {
                datas.clear();
                String jdid = String.valueOf(SPUtils.get(context, value, ""));
                datas.addAll(SgDBUtils.getCScheduleReportAll(jdid));
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }
}
